package cn.bidlink.codegen;

import cn.bidlink.codegen.core.Configeration;
import cn.bidlink.codegen.core.GenService;
import cn.bidlink.codegen.util.Utlis;

import java.io.File;

/**
 * 代码生成器初始化类
 */
public class Application {

    /**
     * 手动创建F:\gen
     */
    public static final String path = "d:\\wangyuzhe\\generator-output\\erdos\\reconciliation";

    public static void main(String[] args) throws Exception {
        createFolder(path);
        String[] tableNames={
               "reconciliation_statement_info",
               "reconciliation_statement_item",
        };
        for (String tableName:tableNames) {
            if(!new File(path+"\\cloudParams\\"+toLowerCaseFirstOne(Utlis.getObjName(tableName))).isDirectory()){
                new File(path+"\\cloudParams\\"+toLowerCaseFirstOne(Utlis.getObjName(tableName))).mkdir();
            }
            codeGen(Utlis.getObjName(tableName), tableName);
        }
    }

    /**
     * 生成器
     *
     * @param objName   实体名称例如（Faq）
     * @param tableName 生成的表名称（bmpfjz_faq）
     */
    public static void codeGen(String objName, String tableName) {
        String rootPath = path+"\\";
        String interfacePath = rootPath + "";
        String extPath = rootPath + "\\ext";
        String mapperPath = rootPath + "";

        String testPath = rootPath + "";

        String entityPath = rootPath + "";

        String implPath = rootPath + "";

        String packageName = "cn.bidlink.reconciliation.manager";

        Configeration configeration = new Configeration();
        GenService genService = new GenService();
        configeration.setDatabaseType(0);
//        configeration.setDatabaseType(1);       // PGSQL数据库
        configeration.setTableName(tableName);/*表名称*/
        configeration.setDaoName(objName + "Dao");/*DAO名称*/
        configeration.setModelName(objName);/*实体类名称*/
        configeration.setLowFirstModelName(toLowerCaseFirstOne(objName));
        configeration.setDubboServiceImplName(objName + "ServiceImpl");/*Dubbo服务的实现类名称*/
        configeration.setDubboServiceName(objName + "Service");/*Dubbo接口名称*/
        configeration.setPackageName(packageName+".dal");/*默认包名称*/
        configeration.setCloudPackageName(packageName+".cloud");/*默认cloud包名*/
        configeration.setAppPackageName(packageName+".app");/*默认app包名*/
        configeration.setAuthor("<a href=\"mailto:yuzhewang@ebnew.com\">wangyuzhe</a>");/*作者*/
        genService.setConfigeration(configeration);

        /*生成Mybatis代码*/
        genService.genXml(mapperPath + "\\sql-mapper\\" + objName + "Mapper.xml");
        genService.genDao(implPath + "dao\\" + objName + "Dao.java");
        genService.genModel(entityPath + "entity\\" + objName + ".java");
        genService.genEnum(entityPath + "enum\\" + "Enum"+objName + ".java");
        /*生成ext代码*/
        genService.genDaoExt(extPath+"\\dao\\"+objName+"ExtDao.java");
        genService.genEntityExt(extPath+"\\entity\\"+objName+"Ext.java");
        genService.genmapperXmlExt(extPath+"\\sql-mapper\\"+objName+"ExtMapper.xml");
        /*生成配置Mybatis-config文件*/
        genService.genSpringConfig(rootPath+"spring-config.xml");
        genService.genSpringConfig2(rootPath+"spring-config2.xml");
        /*生成Dubbo代码和单元测试代码*/
        genService.genDubboService(interfacePath + "dubboService\\" + "Dubbo"+objName + "Service.java");
        genService.genDubboServiceImpl(implPath + "dubboServiceimpl\\" +"Dubbo" +objName + "ServiceImpl.java");
        genService.genTestDubboService(testPath + "test\\" +"Dubbo"+objName + "ServiceTest.java");
        /*生成cloud代码*/
        genService.genCloudService(interfacePath + "cloudService\\" + objName + "Service.java");
        genService.genCloudServiceImpl(implPath + "cloudServiceimpl\\" + objName + "ServiceImpl.java");
        genService.genCloudAddPrams(interfacePath + "cloudParams\\"+objName+"\\" + objName + "AddParams.java");
        genService.genCloudQueryParams(interfacePath + "cloudParams\\"+objName+"\\" + objName + "PageQueryParams.java");
        genService.genCloudUpdateParams(interfacePath + "cloudParams\\"+objName+"\\" + objName + "UpdateParams.java");
        genService.genCloudResponse(implPath + "cloudResponse\\" + objName + "Response.java");
        genService.genCloudFeignService(interfacePath + "cloudFeign\\" + objName + "FeignService.java");
        genService.genCloudController(implPath + "cloudController\\" + objName + "Controller.java");
        genService.genAppController(implPath + "appController\\" + objName + "Controller.java");

    }

    private static void createFolder(String path) {
        File file = new File(path);
        boolean directory = file.isDirectory();
        if(!directory){
            file.mkdirs();
        }

        if(!new File(path+"\\dao").isDirectory()){
            new File(path+"\\dao").mkdir();
        }
        if(!new File(path+"\\enum").isDirectory()){
            new File(path+"\\enum").mkdir();
        }
        if(!new File(path+"\\ext").isDirectory()){
            new File(path+"\\ext").mkdir();
        }
        if(!new File(path+"\\ext\\dao").isDirectory()){
            new File(path+"\\ext\\dao").mkdir();
        }
        if(!new File(path+"\\ext\\entity").isDirectory()){
            new File(path+"\\ext\\entity").mkdir();
        }
        if(!new File(path+"\\ext\\sql-mapper").isDirectory()){
            new File(path+"\\ext\\sql-mapper").mkdir();
        }
        if(!new File(path+"\\dubboService").isDirectory()){
            new File(path+"\\dubboService").mkdir();
        }
        if(!new File(path+"\\dubboServiceimpl").isDirectory()){
            new File(path+"\\dubboServiceimpl").mkdir();
        }
        if(!new File(path+"\\cloudService").isDirectory()){
            new File(path+"\\cloudService").mkdir();
        }
        if(!new File(path+"\\cloudServiceimpl").isDirectory()){
            new File(path+"\\cloudServiceimpl").mkdir();
        }
        if(!new File(path+"\\cloudParams").isDirectory()){
            new File(path+"\\cloudParams").mkdir();
        }
        if(!new File(path+"\\cloudResponse").isDirectory()){
            new File(path+"\\cloudResponse").mkdir();
        }
        if(!new File(path+"\\cloudFeign").isDirectory()){
            new File(path+"\\cloudFeign").mkdir();
        }
        if(!new File(path+"\\cloudController").isDirectory()){
            new File(path+"\\cloudController").mkdir();
        }
        if(!new File(path+"\\appController").isDirectory()){
            new File(path+"\\appController").mkdir();
        }
        if(!new File(path+"\\test").isDirectory()){
            new File(path+"\\test").mkdir();
        }
        if(!new File(path+"\\entity").isDirectory()){
            new File(path+"\\entity").mkdir();
        }
        if(!new File(path+"\\sql-mapper").isDirectory()){
            new File(path+"\\sql-mapper").mkdir();
        }

    }

    public static String toLowerCaseFirstOne(String s){
        if(Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }

}
